//
//  DetailViewController.m
//  ContactsCoreData
//
//  Created by dllo on 15/11/20.
//  Copyright © 2015年 张树林. All rights reserved.
//

#import "SLDetailViewController.h"
#import "SLPerson.h"
#import "SLCoreDataHandler.h"
#import "SLMapController.h"
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件



@interface SLDetailViewController () <BMKGeoCodeSearchDelegate>

@property (weak, nonatomic) IBOutlet UITextField *nameText;
@property (weak, nonatomic) IBOutlet UITextField *phoneText;
@property (weak, nonatomic) IBOutlet UITextField *sexText;
@property (weak, nonatomic) IBOutlet UITextField *numberText;
@property (weak, nonatomic) IBOutlet UITextField *QQText;
@property (weak, nonatomic) IBOutlet UITextField *addressText;

@property (assign, nonatomic) CLLocationCoordinate2D location;
@property (copy, nonatomic) NSString *address;
@property (strong, nonatomic) BMKGeoCodeSearch *searcher;

- (IBAction)mapButton:(UIButton *)sender;
- (IBAction)saveButton:(UIButton *)sender;


@end

@implementation SLDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSLog(@"%@", self.model.name);
    
    [self createData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)searchAddress:(NSString *)address
{
    //初始化检索对象
    _searcher =[[BMKGeoCodeSearch alloc]init];
    _searcher.delegate = self;
    BMKGeoCodeSearchOption *geoCodeSearchOption = [[BMKGeoCodeSearchOption alloc]init];
    geoCodeSearchOption.city= address;
    geoCodeSearchOption.address = address;
    BOOL flag = [_searcher geoCode:geoCodeSearchOption];

    if(flag)
    {
        NSLog(@"geo检索发送成功");
    }
    else
    {
        NSLog(@"geo检索发送失败");
    }
}


- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
        self.location = result.location;
        self.address = result.address;
    }
    else {
        NSLog(@"抱歉，未找到结果");
    }
}


- (void)createData
{
    self.nameText.text = self.model.name;
    self.phoneText.text = self.model.phoneNumber;
    self.sexText.text = self.model.sex;
    self.numberText.text = self.model.number;
    self.QQText.text = self.model.qq;
    self.addressText.text = self.model.address;
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"map"]) {
        [self searchAddress:self.model.address];
        
        SLMapController *mapVC = segue.destinationViewController;
        mapVC.location = self.location;
        mapVC.address = self.address;
    }
}


#pragma mark - view action

- (IBAction)mapButton:(UIButton *)sender {
}


- (IBAction)saveButton:(UIButton *)sender {
    
    SLPerson *con = [[SLPerson alloc] init];
    
    con.name = self.nameText.text;
    con.sex = self.sexText.text;
    con.phoneNumber = self.phoneText.text;
    con.number = self.numberText.text;
    con.qq = self.QQText.text;
    con.address = self.addressText.text;
    
    if ([SLCoreDataHandler hasContact:con]) {
        [SLCoreDataHandler updateWithContact:con];
    } else {
        [SLCoreDataHandler addWithContact:con];        
    }
    
}
@end
